<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Teacher extends Model
{
    use SoftDelete;
    // 连接数据库名称
    //protected $connection = 'db2';
    // 表名
    protected $name = 'teacher';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'created_at';
    protected $updateTime = false;
    protected $deleteTime = 'deleted_at';

    protected static function init()
    {
        self::beforeInsert(function ($row) {
            if (isset($row['avatar']) && $row['avatar']) {
                $row->avatar_updated_at = time();
            }
        });

        self::beforeUpdate(function ($row) {
            $changed = $row->getChangedData();
            //如果有修改头像
            if (isset($changed['avatar']) && $changed['avatar']) {
                $row->avatar_updated_at = time();
            }
        });
    }


    public function getOriginData()
    {
        return $this->origin;
    }

    public function school()
    {
        return $this->belongsTo('School');
    }

    public function user()
    {
        return $this->belongsTo('User');
    }

    public function getGenderTextAttr($value, $data)
    {
        return User::getGender($data['gender']);
    }

}
